Online-Academy
Look, Read, Understand, Apply

Indentation in Python

Indentation in Python refers to the spaces (or tabs) at the beginning of a line of code. Unlike many other programming languages, indentation in Python is not just for readability — it is a part of the syntax.

Indentation Defines Code Blocks

Python uses indentation to group statements together.

  • In other languages (C, Java), { } are used
  • In Python, indentation replaces { }
    if age >= 18:
        print("You are eligible to vote")
        print("Please carry your ID")

Both print statements belong to the if block because they are indented.

Without Indentation, Code Will Not Run
Wrong:
    if age >= 18:
    print("Eligible")

Correct:

if age >= 18:
    print("Eligible")

Used in All Control Structures

def greet():
    print("Hello")
    print("Welcome to Python")


Both lines are part of the function because of indentation.

Logical Errors Can Occur Due to Wrong Indentation.

Wrong Logic:

if marks >= 40:
    print("Pass")
print("Exam Finished")

"Exam Finished" runs always, not conditionally.
  • Indentation is mandatory in Python
  • It defines code blocks
  • Replaces { } used in other languages
  • Improves readability and program structure
  • Incorrect indentation causes IndentationError
  • Standard indentation is 4 spaces